package com.dragon.annotation;

import java.util.*;

/**
 * 默认的生成sql实现类
 *
 * @author dragon
 * @since 2019/11/12
 */
public class DefaultGenSql implements GenSql {

    private void generateColumnsSql(EntityTable entityTable, StringBuilder sql) {
        LinkedHashSet<EntityColumn> entityClassColumnList = entityTable.getEntityClassColumns();
        int count = 0;
        boolean isLastColumn = false;

        for (EntityColumn entityColumn : entityClassColumnList) {
            if (count == (entityClassColumnList.size() - 1)) {
                isLastColumn = true;
            }

            count++;
            generateSingleColumnSql(entityColumn, entityTable.getUniqueConstraintColumnList(), sql, isLastColumn);
        }
    }

    private void generatePrimaryKeySql(EntityTable entityTable, StringBuilder sql) {
        sql.append(entityTable.getPrimaryKeyName())
                .append(GenSql.BLANK)
                .append(GenSql.DEFAULT_PRIMARY_KEY_TYPE)
                .append(GenSql.BLANK)
                .append(GenSql.PRIMARY_KEY)
                .append(GenSql.BLANK);

        if (entityTable.isAutoIncrement()) {
            sql.append(GenSql.AUTO_INCREMENT);
        }

        sql.append(GenSql.COMMA);
    }

    private void generateSingleColumnSql(EntityColumn entityColumn, List<String> uniqueConstraintColumnList,
                                         StringBuilder sql, boolean isLastColumn) {
        sql.append(entityColumn.getName()).append(GenSql.BLANK).append(entityColumn.getColumnDefinition());

        if (!entityColumn.getNullable()) {
            sql.append(GenSql.BLANK).append(GenSql.NOT_NULL);
        }

        if (uniqueConstraintColumnList.contains(entityColumn.getName())) {
            sql.append(GenSql.BLANK).append(GenSql.UNIQUE);
        }

        if (!isLastColumn) {
            sql.append(GenSql.COMMA);
        }
    }

    /**
     * 生成sql语句
     *
     * @param entityTable 数据库表
     * @return java.lang.String sql语句
     */
    @Override
    public String generateSql(EntityTable entityTable) {
        if (entityTable == null) {
            return null;
        }

        // 表名
        StringBuilder sql = generateTableHeadSql(entityTable);

        // 主键
        generatePrimaryKeySql(entityTable, sql);

        // 列
        generateColumnsSql(entityTable, sql);

        // 表结尾
        generateTableTailSql(sql);

        return sql.toString();
    }

    @Override
    public String generateSql(List<EntityTable> entityTableList) {
        if (entityTableList == null && entityTableList.size() == 0) {
            return null;
        }

        StringBuilder resultSql = new StringBuilder();

        for (EntityTable entityTable : entityTableList) {
            resultSql.append(generateSql(entityTable));
        }

        return resultSql.toString();
    }

    private StringBuilder generateTableHeadSql(EntityTable entityTable) {
        StringBuilder sql = new StringBuilder(GenSql.CREATE_TABLE);

        sql.append(GenSql.BLANK).append(entityTable.getName()).append(GenSql.BRACKET);

        return sql;
    }

    private void generateTableTailSql(StringBuilder sql) {
        sql.append(GenSql.ANTI_BRACKET)
                .append(GenSql.DEFAULT_ENGINE)
                .append(GenSql.BLANK)
                .append(GenSql.DEFAULT_ENCODING)
                .append(GenSql.DELIMETER);
    }
}

